package net.petiwala.Tasbeeh;



import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.media.AudioManager;
import android.os.Bundle;
import android.os.Vibrator;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.SlidingDrawer;
import android.widget.SlidingDrawer.OnDrawerCloseListener;
import android.widget.TextView;
import android.widget.Toast;

import com.google.ads.AdRequest;
import com.google.ads.AdSize;
import com.google.ads.AdView;

public class Tasbeeh extends Activity {

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		Log.d(tag, "## OnResume()   ##");

		// resetting sound icon and settings
		if (preferences.getBoolean("disableSound", false) == false) {
			isSoundEnable = true;
			buttonSound.setBackgroundResource(R.drawable.ic_jog_dial_sound_on);
		} else {
			isSoundEnable = false;
			buttonSound.setBackgroundResource(R.drawable.ic_jog_dial_sound_off);
		}
		// resetting vibrate icon and settings.
		if (preferences.getBoolean("disableVibrate", false) == false) {
			isVibrateEnable = true;
			buttonVibrate
					.setBackgroundResource(R.drawable.ic_jog_dial_vibrate_on);
		} else {
			isVibrateEnable = false;
			buttonVibrate
					.setBackgroundResource(R.drawable.ic_jog_dial_vibrate_off);
		}

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
		// return super.onCreateOptionsMenu(menu);
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.menu, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {

		// Log.d(tag,"Menu Selected: "+ item.getItemId() );
		// Log.d(tag,"Menu prefs: "+ R.id.menuPreferences );
		// Log.d(tag,"Menu select tasbih: "+ R.id.menuSelectTasbeeh );

		switch (item.getItemId()) {
		/*
		 * 
		 * Tasbeeh Selection logic
		 */
		case R.id.menuSelectTasbeeh:
			// add tasbeeh screen here

			Builder builder = new AlertDialog.Builder(this);
			// builder.setMessage("Select Tasbeeh");
			builder.setSingleChoiceItems(AzkarList, AzkarSelected,
					new DialogInterface.OnClickListener() {

						//@Override
						public void onClick(DialogInterface dialog, int which) {
							// TODO Auto-generated method stub
							Log.d(tag, "which: " + which);
							AzkarSelected = which;
						}
					});

			builder.setNeutralButton("Select",
					new DialogInterface.OnClickListener() {

						//@Override
						public void onClick(DialogInterface dialog, int which) {
							// TODO Auto-generated method stub
							Log.d(tag, "Button which: " + which);
							switch (AzkarSelected) {

							case 0:
								zikrImage
										.setImageResource(R.drawable.tasbeeh_fatima);
								break;
							case 1:
								zikrImage.setImageResource(R.drawable.zikr1);
								break;
							case 2:
								zikrImage.setImageResource(R.drawable.zikr2);
								break;
							case 3:
								zikrImage.setImageResource(R.drawable.zikr3);
								break;
							case 4:
								zikrImage.setImageResource(R.drawable.zikr4);
								break;
							case 5:
								zikrImage.setImageResource(R.drawable.zikr5);
								break;
							case 6:
								zikrImage.setImageResource(R.drawable.zikr6);
								break;
							case 7:
								zikrImage.setImageResource(R.drawable.zikr7);
								break;

							}

						}

					});

			AlertDialog dialog = builder.create();
			dialog.show();

			break;

		/*
		 * Preferences Menu Logic *
		 */
		case R.id.menuPreferences:
			Intent i = new Intent(Tasbeeh.this, Preferences.class);
			startActivity(i);
			Toast.makeText(Tasbeeh.this, "Here you can enter your Prefrences",
					Toast.LENGTH_LONG).show();

			break;

		}

		return true;
	}

	String tag = "TASBEEH";
	TextView textDummy;
	Button handle, buttonSound, buttonVibrate, buttonReset;
	SlidingDrawer slidingDrawer1;
	RelativeLayout relativeLayout1;
	private SoundManager mSoundManager;
	int i = 0;
	Boolean isSoundEnable, isVibrateEnable;
	SharedPreferences preferences;
	String AzkarList[] = {
			"Tasbeeh-e-Fatima",
			"SubhanAllahi'l-adheem wa biHamdihi",
			"La Hawla wa la Quwatta illa Billah",
			"SubhanAllah, walHamdulilah, wa La illaha ilAllahu, waAllahu Akbar",
			"Astaghfirullah al-'Adheemal-ladhi la ilaha illa Huwal-Hayyul-Qayyum wa atubu ilaih",
			"La illaha ilAllah(u)", 
			"SubhanAllah", 
			"SubhanAllahi wa biHamdihi" };
	int AzkarSelected = 0;
	ImageView zikrImage;
	float fromX, fromY, toX, toY;
	boolean isActionDown = false;
	String gesture;
	int MaxOffPath = 5;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		init(); // get the all the fields initialized

		// this is to setup Media sound as default for volume keys.
		setVolumeControlStream(AudioManager.STREAM_MUSIC);

		// ******* AdMob Code Start
		String MY_AD_UNIT_ID = "a14dc9b060511af";
		AdView adView = new AdView(this, AdSize.BANNER, MY_AD_UNIT_ID);
		LinearLayout layout = (LinearLayout) findViewById(R.id.mainLayout);
		layout.addView(adView);
		AdRequest myAd = new AdRequest();
		myAd.setTesting(false);
		adView.loadAd(myAd);
		// ******* AdMob Code END

		buttonReset.setOnClickListener(new OnClickListener() {

			//@Override
			public void onClick(View v) {
				// Reset Tasbeeh Counter

				DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
					//@Override
					public void onClick(DialogInterface dialog, int which) {
						switch (which) {
						case DialogInterface.BUTTON_POSITIVE:
							// Yes button clicked
							i = 0;

							if (isVibrateEnable == true) {
								((Vibrator) getSystemService(VIBRATOR_SERVICE))
										.vibrate(50);
							}

							if (isSoundEnable == true) {
								mSoundManager.playSound(1);
							}

							textDummy.setText(String.valueOf(i));
							slidingDrawer1.open();
							handle.setText(String.valueOf(i));

							break;

						case DialogInterface.BUTTON_NEGATIVE:
							// No button clicked [do nothing]
							break;
						}
					}
				};

				AlertDialog.Builder builder = new AlertDialog.Builder(
						Tasbeeh.this);
				builder.setMessage("Are you sure?")
						.setPositiveButton("Yes", dialogClickListener)
						.setNegativeButton("No", dialogClickListener).show();

			}
		});

		buttonVibrate.setOnClickListener(new OnClickListener() {

			//@Override
			public void onClick(View v) {
				Editor edit = preferences.edit();

				// Vibrate button onClick
				if (isVibrateEnable == true) {
					Toast.makeText(Tasbeeh.this, R.string.strVibrateOff,
							Toast.LENGTH_SHORT).show();
					isVibrateEnable = false;
					edit.putBoolean("disableVibrate", true);
					buttonVibrate
							.setBackgroundResource(R.drawable.ic_jog_dial_vibrate_off);
				} else {
					Toast.makeText(Tasbeeh.this, R.string.strVibrateOn,
							Toast.LENGTH_SHORT).show();
					isVibrateEnable = true;
					edit.putBoolean("disableVibrate", false);
					buttonVibrate
							.setBackgroundResource(R.drawable.ic_jog_dial_vibrate_on);
				}

				edit.commit();

				Log.d(tag, "Vibrate Clicked: Vibrate" + isVibrateEnable);

			}
		});

		buttonSound.setOnClickListener(new OnClickListener() {

			//@Override
			public void onClick(View v) {
				Editor edit = preferences.edit();

				// Sound Button onClick
				Log.d(tag, "######### Sound Button onClick ###########");
				if (isSoundEnable == true) {
					Toast.makeText(Tasbeeh.this, R.string.strSoundOff,
							Toast.LENGTH_SHORT).show();
					isSoundEnable = false;
					edit.putBoolean("disableSound", true);
					buttonSound
							.setBackgroundResource(R.drawable.ic_jog_dial_sound_off);
				} else {
					Toast.makeText(Tasbeeh.this, R.string.strSoundOn,
							Toast.LENGTH_SHORT).show();
					isSoundEnable = true;
					edit.putBoolean("disableSound", false);
					buttonSound
							.setBackgroundResource(R.drawable.ic_jog_dial_sound_on);
				}
				edit.commit();
				Log.d(tag, "Sound Clicked: Sound" + isSoundEnable);
			}
		});

		mSoundManager = new SoundManager();
		mSoundManager.initSounds(getBaseContext());
		mSoundManager.addSound(1, R.raw.click17c);

		textDummy.setText(String.valueOf(i));

		slidingDrawer1.open();
		
		slidingDrawer1.setOnDrawerCloseListener(new OnDrawerCloseListener() {

			//@Override
			public void onDrawerClosed() {

				if (isVibrateEnable == true) {
					((Vibrator) getSystemService(VIBRATOR_SERVICE)).vibrate(50);
				}
				Log.d(tag, "on Drawer: Sound: " + isSoundEnable);
				if (isSoundEnable == true) {
					mSoundManager.playSound(1);
					Log.d(tag, "After sound Play: Sound: " + isSoundEnable);
				}

				i++;
				textDummy.setText(String.valueOf(i));
				slidingDrawer1.open();
				handle.setText(String.valueOf(i));

			}

		});

		
		
		relativeLayout1.setOnTouchListener(new OnTouchListener(){

			public boolean onTouch(View arg0, MotionEvent event) {
								slidingDrawer1.animateClose();
								//slidingDrawer1.
								
				
				return false;
			}} );
	}
	

	private void init() {

		Log.d(tag, "######### init() ###########");

		textDummy = (TextView) findViewById(R.id.textDummy);
		handle = (Button) findViewById(R.id.handle);
		slidingDrawer1 = (SlidingDrawer) findViewById(R.id.slidingDrawer1);
		relativeLayout1 = (RelativeLayout) findViewById(R.id.relativeLayout1);
		buttonSound = (Button) findViewById(R.id.buttonSound);
		buttonVibrate = (Button) findViewById(R.id.buttonVibrate);
		buttonReset = (Button) findViewById(R.id.buttonReset);
		zikrImage = (ImageView) findViewById(R.id.tasbeehImage);

		// Initialize preferences
		preferences = PreferenceManager.getDefaultSharedPreferences(this);

		// setting Sound
		if (preferences.getBoolean("disableSound", false) == false) {
			isSoundEnable = true;
			buttonSound.setBackgroundResource(R.drawable.ic_jog_dial_sound_on);
		} else {
			isSoundEnable = false;
			buttonSound.setBackgroundResource(R.drawable.ic_jog_dial_sound_off);
		}
		// Setting vibrate option
		if (preferences.getBoolean("disableVibrate", false) == false) {
			isVibrateEnable = true;
			buttonVibrate
					.setBackgroundResource(R.drawable.ic_jog_dial_vibrate_on);
		} else {
			isVibrateEnable = false;
			buttonVibrate
					.setBackgroundResource(R.drawable.ic_jog_dial_vibrate_off);
		}

	}
}